<template>
  <div class="my-tag">
    <input
      class="input"
      type="text"
      placeholder="输入标签"
      v-if="isShow"
      @keydown.enter="changeTag"
      ref="tag"
      v-focus
      @blur="isShow = false"
    />
    <div class="text" v-else @dblclick="show">{{ value }}</div>
  </div>
</template>

<script>
export default {
  name: "MyTag",
  props: {
    value: {
      required: true,
      type: String,
    },
  },
  data() {
    return {
      isShow: false,
    };
  },

  methods: {
    changeTag() {
      this.$nextTick(() => {
        this.$emit("input", this.$refs.tag.value);
        this.isShow = false;
      });
    },
    show() {
      this.isShow = true;
      this.$nextTick(() => {
        this.$refs.tag.value = this.value;
      });
    },
  },
};
</script>

<style lang="less" scoped>
.my-tag {
  cursor: pointer;
  .input {
    appearance: none;
    outline: none;
    border: 1px solid #ccc;
    width: 100px;
    height: 40px;
    box-sizing: border-box;
    padding: 10px;
    color: #666;
    &::placeholder {
      color: #666;
    }
  }
}
</style>